package com.mobartisan.vehiclenetstore.util;

import android.util.Log;
import android.webkit.JavascriptInterface;

import com.igexin.sdk.PushManager;
import com.mobartisan.vehiclenetstore.ECarApplication;
import com.mobartisan.vehiclenetstore.constants.Constants;
import com.mobartisan.vehiclenetstore.network.action.model.ECarActionmpl;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;

import static com.igexin.sdk.GTServiceManager.context;


/**
 * Created by L on 2017/6/26.
 */

public class JsToJava {
    private String TAG = JsToJava.class.getSimpleName();
    //由于安全原因 需要加 @JavascriptInterface
    @JavascriptInterface
    public void jsMethod(String result) {

            new Thread(new Runnable() {
                @Override
                public void run() {
                    Log.d(TAG, "js返回结果线程下:" + result);
                    String clid = SharedPreferencesUtil.getInstance().getStringValue("client_id");
                    if (ObjectUtils.isStringEmpty(clid)){
                        getClientId();
                    }
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(result);
                        String userid = jsonObject.getString("user_id");
                        long user_id = Integer.valueOf(userid);
                        String client_id = SharedPreferencesUtil.getInstance().getStringValue("client_id");
                        ECarActionmpl eCarActionmpl = new ECarActionmpl();
                        Log.d(TAG, "js返回结果线程下:=======" + user_id+"========="+client_id);

                        eCarActionmpl.upload(user_id, client_id, new Observer<ResponseBody>() {
                            @Override
                            public void onSubscribe(@NonNull Disposable d) {
                                Log.d(TAG, "onSubscribe===========" + d);
                            }

                            @Override
                            public void onNext(@NonNull ResponseBody responseBody) {
                                try {
                                    Log.d(TAG, "onNext===========" + responseBody.string());
                                    String response = responseBody.string();
                                    JSONObject jsonObject1 = new JSONObject(response);
                                    String code = jsonObject1.getString("code");
                                    if (!code.equals("200")){
                                        ToastUtils.showShort("上传clientId失败");
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onError(@NonNull Throwable e) {
                                ToastUtils.showShort("上传clientId失败");
                                Log.d(TAG, "onError===========" + e.getMessage());
                            }

                            @Override
                            public void onComplete() {
                                Log.d(TAG, "onComplete===========");
                            }
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }


            }).start();
        }

    private void getClientId() {
        String client_id = PushManager.getInstance().getClientid(ECarApplication.getInstance());
        Log.d(TAG + "client_id==========", ""+client_id);
        SharedPreferencesUtil.getInstance().setStringValue("client_id", client_id);
    }



    @JavascriptInterface
    public void pushMethod(String result) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, "run: ================pushMethod"+result);
                if (result.equals("onpush")){
                    SharedPreferencesUtil.getInstance().setStringValue(Constants.On_Off_Push,result);
                    PushManager.getInstance().turnOnPush(ECarApplication.getInstance());
                }else if (result.equals("offpush")){
                    SharedPreferencesUtil.getInstance().setStringValue(Constants.On_Off_Push,result);
                    PushManager.getInstance().turnOffPush(ECarApplication.getInstance());
                }
            }
        }).start();

    }

}
